import argparse
from functools import cached_property
from hmac import new
from re import S
import polars as pl
from pydantic import BaseModel
from pysam import VariantFile, VariantRecord


class Check(BaseModel):
    old_vcf: str
    new_vcf: str

    @classmethod
    def check(cls, variant: VariantRecord, variant_file: VariantFile) -> list[str]:
        for record in variant_file.fetch(variant.chrom, variant.start, variant.stop):
            if record.chrom == variant.chrom and record.pos == variant.pos and record.ref == variant.ref and record.alts == variant.alts:
                text = f"{variant.chrom}\t{variant.pos}\t{variant.ref}\t{variant.alts[0]}"
                for key in ["GENE", "EVENT", "REGION", "DETAIL"]:
                    text += "\t" + (",".join(variant.info.get(key, [])) or ".")
                    text += "\t" + (",".join(record.info.get(key, [])) or ".")
                print(text)

    def run(self):
        variant_file = VariantFile(self.old_vcf)
        for variant in VariantFile(self.new_vcf):
            self.check(variant, variant_file)


def main():
    parser = argparse.ArgumentParser(description="OpenAnno SNV")
    parser.add_argument("-old", "--old_vcf", required=True, help="input, old VCF file")
    parser.add_argument("-new", "--new_vcf", required=True, help="input, new VCF file")
    parser.set_defaults(func=lambda args: Check.model_validate(vars(args)).run())
    args = parser.parse_args()
    args.func(args)


if __name__ == "__main__":
    main()
